package uilayer;

import java.util.InputMismatchException;
import java.util.Scanner;
import modellayer.Item;
import controllayer.*;

/**
 * Textbased UI which allows the user to input information about items.
 *@author Benjamin Ørskov
 *@author Lasse Lambertsen
 *@author Ole Kirk
 *@author Søren Søgaard
 */
public class ItemUI extends MenuInputs {

	// instance variables - object by the type iCtrl
	/** The item ctrl. */
	private ItemController iCtrl;

	/** The instance. */
	private static ItemUI instance;

	/**
	 * Constructor for ItemUI.
	 */
	private ItemUI() {
		// Initialize iCtrl
		iCtrl = new ItemController();
	}

	/**
	 * Gets the single instance of ItemUI.
	 *
	 * @return single instance of ItemUI
	 */
	public static ItemUI getInstance() {
		if (instance == null) {
			instance = new ItemUI();
		}
		return instance;
	}

	/**
	 * Item menu.
	 */
	public void itemMenu() {
		boolean exit = false;
		try {
			while (!exit) { // that is: while exit is false
				int choice = writeItemMenu();
				if (choice == 1) { // Create item
					int itemID = inputItemID();
					try {
						iCtrl.findItem(itemID);
					} catch (NullPointerException e) {
						iCtrl.createItem(inputType(), itemID, inputPrice(),
								inputAmount());
					}
					System.out.println("Vare ID er allerede i brug!");

				} else if (choice == 2) { // find item
					try {
						System.out.println(iCtrl.findItem(inputItemID()));
					} catch (NullPointerException e) {
						System.out.println(e.getMessage());
					}
				} else if (choice == 3) {
					System.out.println("Indtast nuværende vare ID: ");
					try {
						Item tempI = iCtrl.findItem(inputItemID());
						System.out.println("Indtast nye oplysninger: ");
						try {
							iCtrl.updateItem(tempI, inputType(), inputItemID(),
									inputPrice(), inputAmount());
						} catch (UpdateIncompleteException uie) {
							System.out.println(uie.getMessage());
						}
					} catch (NullPointerException e) {
						System.out.println(e.getMessage());
					}
				} else if (choice == 4) {
					try {
						iCtrl.removeItem(inputItemID());
					} catch (NullPointerException e) {
						System.out.println(e.getMessage());
					}
				} else {
					exit = true;
				} // end else
			} // end while
		} // end try
		catch (InputMismatchException e) {
			// do something clever with the exception
			System.out.println("Du skal indtaste et tal!");
			itemMenu();
		}
	}

	/**
	 * Display item menu.
	 *
	 * @return the int
	 */
	private int writeItemMenu() { // makes an object keyboard to read input from
		// the screen
		Scanner keyboard = new Scanner(System.in);
		System.out.println("*** Vare Menu ***");
		System.out.println(" (1) Tilf�j vare");
		System.out.println(" (2) Find vare");
		System.out.println(" (3) Rediger vare");
		System.out.println(" (4) Slet vare");
		System.out.println(" (5) Tilbage");
		System.out.print("\n Vælg: ");

		int choice = keyboard.nextInt();
		return choice;
	}
}